import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:easy_go/data/model/my_model.dart';

const baseUrl = 'https://jsonplaceholder.typicode.com/posts/';

class MyApiClient {
  //
  final Dio dio = Dio();
  //
  getAll() async {
    try {
      var response = await dio.get(baseUrl);
      if (response.statusCode == 200) {
        Iterable jsonResponse = response.data;
        // Iterable jsonResponse = json.decode(response.data);
        List<MyModel> listModel = jsonResponse.map((e) => MyModel.fromJson(e)).toList();
        return listModel;
      } else {
        debugPrint('error: ${response.statusCode}');
      }
    } catch(e) {
      debugPrint(e.toString());
    }
  }
  //
  getId(id) async {
    try {
      var response = await dio.get(baseUrl);
      if (response.statusCode == 200) {
        Map<String, dynamic> jsonResponse = json.decode(response.data);
        return jsonResponse;
      } else {
        debugPrint('error~');
      }
    } catch(e) {
      debugPrint(e.toString());
    }
  }
}
